# -*- encoding:utf-8 -*-
# __author__ = 'chenyongbing'
# __poject__ = 'ops_platform'
# __datetime__ = '2022/7/26'
# __name__ = 'cache.py'
from django.core.cache import cache
from apps.resource.models import Business, Project, Service, Environment, Kubernetes, DockerRepo
from apps.cicd.models import Capacity, YAMLTemplate
# Create your code here.

### cache

def get_business_name_cache(id):
    if not id: return None
    key = "resource:business:{}:name".format(id)
    value = cache.get(key)
    if not value:
        obj = Business.objects.get(id=id)
        value = obj.code
        cache.set(key, value, 60 * 24 * 7)
    return value

def get_business_name_cache_by_service(id):
    if not id: return None
    key = "resource:service:{}:business:name".format(id)
    value = cache.get(key)
    if not value:
        obj = Service.objects.get(id=id)
        value = obj.business.name
        cache.set(key, value, 60 * 24 * 7)
    return value

def get_project_name_cache(id):
    if not id: return None
    key = "resource:project:{}:name".format(id)
    value = cache.get(key)
    if not value and id:
        obj = Project.objects.get(id=id)
        value = obj.code
        cache.set(key, value, 60 * 24 * 7)
    return value


def get_service_name_cache(id):
    if not id: return None
    key = "resource:service:{}:name".format(id)
    value = cache.get(key)
    if not value:
        obj = Service.objects.get(id=id)
        value = obj.code
        cache.set(key, value, 60 * 24 * 7)
    return value


def get_env_name_cache(id):
    if not id: return None
    key = "resource:env:{}:name".format(id)
    value = cache.get(key)
    if not value:
        obj = Environment.objects.get(id=id)
        value = obj.name
        cache.set(key, value, 60 * 24 * 7)
    return value


def get_kube_name_cache(id):
    if not id: return None
    key = "resource:kube:{}:name".format(id)
    value = cache.get(key)
    if not value:
        obj = Kubernetes.objects.get(id=id)
        value = obj.name
        cache.set(key, value, 60 * 24 * 7)
    return value

def get_repo_name_cache(id):
    if not id: return None
    key = "resource:repo:{}:name".format(id)
    value = cache.get(key)
    if not value:
        obj = DockerRepo.objects.get(id=id)
        value = obj.name
        cache.set(key, value, 60 * 24 * 7)
    return value

def get_capacity_name_cache(id):
    if not id: return None
    key = "resource:capacity:{}:name".format(id)
    value = cache.get(key)
    if not value:
        obj = Capacity.objects.get(id=id)
        value = obj.name
        cache.set(key, value, 60 * 24 * 7)
    return value

def get_template_name_cache(id):
    if not id: return None
    key = "resource:template:{}:name".format(id)
    value = cache.get(key)
    if not value:
        obj = YAMLTemplate.objects.get(id=id)
        value = obj.name
        cache.set(key, value, 60 * 24 * 7)
    return value

###
def set_deployment_info_cache(service, env, info, ttl=60 * 10):
    key = 'cicd:kube:env:{}:deploy:{}:info'.format(env, service)
    cache.set(key, info, ttl)

    return True


def get_deployment_info_cache(service, env):
    key = 'cicd:kube:env:{}:deploy:{}:info'.format(env, service)
    return cache.get(key)



def set_pod_info_cahce(kube, namespace, info, ttl=15):
    key = 'cicd:kube:{}:namespace:{}:pods:info'.format(kube, namespace)
    cache.set(key, info, ttl)
    return True

def get_pod_info_cahce(kube, namespace):
    key = 'cicd:kube:{}:namespace:{}:pods:info'.format(kube, namespace)
    return cache.get(key)


def set_deployment_exists_cache(service, env, value, ttl=60 * 60 * 24):
    key = 'cicd:kube:env:{}:deploy:{}:exists'.format(env, service)
    cache.set(key, value, ttl)
    return True

def get_deployment_exists_cache(service, env):
    key = 'cicd:kube:env:{}:deploy:{}:exists'.format(env, service)
    return cache.get(key)
